package com.frank.oj.controller.admin;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.frank.oj.common.exception.StatusFailException;
import com.frank.oj.common.result.CommonResult;
import com.frank.oj.model.dto.AdminEditUserDTO;
import com.frank.oj.model.vo.UserRolesVO;
import com.frank.oj.service.admin.account.UserService;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author frank
 * @Date 2024/3/27
 */
@RestController
@RequestMapping("/api/admin/user")
public class UserController {
    @Resource
    private UserService userService;

    /**
     * 查
     */
    @GetMapping("/get-user-list")
    @RequiresAuthentication
    @RequiresPermissions("user_admin")
    public CommonResult<IPage<UserRolesVO>> getUserList(@RequestParam(value = "limit",required = false) Integer limit,
                                                        @RequestParam(value = "currentPage",required = false)Integer currentPage,
                                                        @RequestParam(value = "onlyAdmin",required = false)Boolean onlyAdmin,
                                                        @RequestParam(value = "keyword",required = false)String keyword){
        return CommonResult.successResponse(userService.getUserList(limit,currentPage,onlyAdmin,keyword));
    }

    /**
     * 修改
     * @param adminEditUserDTO 修改信息
     * @return null/异常
     */
    @PutMapping("edit-user")
    @RequiresAuthentication
    @RequiresPermissions("user_admin")
    public CommonResult<Void> editUser(@RequestBody AdminEditUserDTO adminEditUserDTO){
        try {
            userService.editUser(adminEditUserDTO);
            return CommonResult.successResponse();
        } catch (Exception e) {
            return CommonResult.errorResponse(e.getMessage());
        }
    }

    /**
     * 删
     * @param params 一或多
     * @return null/异常
     */
    @DeleteMapping("delete-user")
    @RequiresAuthentication
    @RequiresPermissions("user_admin")
    public CommonResult<Void> deleteUser(@RequestBody Map<String,Object > params){
        try {
            userService.deleteUser(params);
            return CommonResult.successResponse();
        } catch (StatusFailException e) {
            return CommonResult.errorResponse(e.getMessage());
        }
    }

    /**
     * 批量创建
     * @param params 相关信息
     * @return null/异常
     */
    @PostMapping("/insert-batch-user")
    @RequiresPermissions("user_admin")
    @RequiresAuthentication
    public CommonResult<Void> insertBatchUser(@RequestBody Map<String, Object> params) {
        try {
            userService.insertBatchUser((List<List<String>>) params.get("users"));
            return CommonResult.successResponse();
        }catch (StatusFailException e){
            return CommonResult.errorResponse(e.getMessage());
        }
    }

    /**
     * 自动生成
     * @param params 信息
     * @return null/异常
     */
    @PostMapping("/generate-user")
    @RequiresPermissions("user_admin")
    @RequiresAuthentication
    public CommonResult<Map<Object, Object>> generateUser(@RequestBody Map<String, Object> params) {
        try {
            return CommonResult.successResponse(userService.generateUser(params));
        }catch (StatusFailException e){
            return CommonResult.errorResponse(e.getMessage());
        }
    }


}
